<template>
  <div class="index">
    <div class="titleBar">
      <div class="backIcon" @click="back">
        <svg t="1593162984672" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1917" width="220" height="220"><path d="M663.466667 889.6l-362.666667-362.666667c-8.533333-8.533333-8.533333-21.333333 0-29.866666l362.666667-362.666667c8.533333-8.533333 21.333333-8.533333 29.866666 0l29.866667 29.866667c8.533333 8.533333 8.533333 21.333333 0 29.866666L422.4 497.066667c-8.533333 8.533333-8.533333 21.333333 0 29.866666l300.8 300.8c8.533333 8.533333 8.533333 21.333333 0 29.866667l-29.866667 29.866667c-8.533333 10.666667-21.333333 10.666667-29.866666 2.133333z" p-id="1918"></path></svg>
      </div>
      <div class="title">
        图鉴详情
      </div>
    </div>
    <div class="card">
      <div class="cardImg">
        <div class="gem">
          <div class="imgWrap">
            <img :src="cardDetail.img" alt="">
          </div>
        </div>
        <div class="gemDetail">
          <div class="gemTitle">
            {{cardDetail.name}}
          </div>
          <div class="content">
            <div v-html="cardDetail.detail"></div>
          </div>
        </div>
        <div class="coin">
          <span class="_value">{{cardDetail.coin_count}}</span><span class="_text">宝币</span>
        </div>
      </div>
      <div class="cardDesc">
        <dl>
          <dt>收集图鉴途径：</dt>
          <dd>{{cardDetail.memo}}</dd>
        </dl>
      </div>
      <div class="btnWrap" v-show="cardDetail.ratio_desc">
        <div class="btn" :class="{success: cardDetail.ratio_type === 3, collecting: cardDetail.ratio_type === 1}" @click="() => {receiveCard(cardDetail)}">{{cardDetail.ratio_desc}}</div>
      </div>
    </div>
    <div id="animate" v-show="isShowAnimate"></div>
    <transition name="alertAnimate">
      <div class="getSuccess" v-if="isShowAlert">
        <h3>恭喜您获得</h3>
        <div class="rotateBg"></div>
        <div class="gemWrap">
          <div class="gem">
            <div class="gemImgWrap">
              <img :src="currentCard.img" alt="">
            </div>
          </div>
        </div>
        <p class="gemName">{{currentCard.name}}</p>
      </div>
    </transition>
    <div class="confirmBtn" v-if="isShowAlert" @click="closeAnimate">确定</div>
  </div>
</template>
<script>
import {getCardDetail, receiveCard} from '@/api/request'
import { getQueryVariable } from '@/tools/utils'
import lottie from 'lottie-web';
import * as starAnimate from '../../../static/animate/star.json';
export default {
  name: 'card',
  data () {
    return {
      cardDetail: {},
      isShowAnimate: false,
      isShowAlert: false,
      animation: null,
      currentCard: {}
    }
  },
  created () {
    this.cardDetail = {}
    this.getCardDetail()
  },
  mounted () {
    this.loadAnimation(true)
  },
  methods: {
    back () {
      window.WebViewJavascriptBridge && window.WebViewJavascriptBridge.callHandler('goBack')
    },
    closeAnimate () {
      this.isShowAlert = false
      this.isShowAnimate = false
      this.animation.stop()
      this.$store.commit("controlMask", { maskIsShow: false });
    },
    loadAnimation (hide) {
      this.isShowAnimate = true
      if (hide) {
        this.isShowAnimate = false
      }
      console.log(this.animation)
      this.$nextTick(() => {
        if (this.animation) {
          console.log('先停止再播放')
          this.animation.stop()
          this.animation.play()
        } else {
          this.animation = lottie.loadAnimation({
            container: document.getElementById('animate'),
            renderer: 'svg',
            loop: true,
            autoplay: true,
            animationData: starAnimate,
            assetsPath: 'static/animate/images/',
            path: '../../../static/animate/star.json'
          })
          this.animation.addEventListener('loopComplete', () =>{
            // this.isShowAnimate = false
            // this.animation = null
            console.log('停止')
            this.animation.stop()
          })
        }
      })
    },
    getCardDetail () {
      console.log(this.id)
      getCardDetail({
        id: this.$route.query.id
      }).then(res => {
        console.log(res)
        this.cardDetail = res
      }).catch(e => {
        console.log(e)
        if (e == '18009') {
          this.getCardDetail()
        } else {
          this.$toastMessage({message: e.message || e.msg, time: 3000})
        }
      })
    },
    receiveCard (n) {
      this.currentCard = n
      if (n.ratio_type === 2) {
        this.dataCollect('TE_TJCOLLECT_CLLCK', '集图鉴得宝币', {
          ITEM_ID: n.id,
          ITEM_NAME: '收集',
          PAGE_PARAM: n.id
        })
        receiveCard({
          card_id: n.id
        }).then(res => {
          console.log(res)
          this.$store.commit("controlMask", { maskIsShow: true });
          this.isShowAlert = true
          this.loadAnimation()
          this.getCardDetail()
        }).catch(e => {
          if (e == '18009') {
            this.getCardDetail()
          } else {
            this.$toastMessage({message: e.message || e.msg, time: 3000})
          }
        })
      }
    },
  }
}
</script>
<style lang="less" scoped>
  
  .index{
    width: 100%;
    min-height: calc(100vh - constant(safe-area-inset-top));
    min-height: calc(100vh - env(safe-area-inset-top));
    overflow: hidden;
    top: constant(safe-area-inset-top); /* 兼容 iOS < 11.2 */
    top: env(safe-area-inset-top);
    background-color: #942121;
    background: url(../../assets/images/card.jpg) no-repeat;
    background-size: cover;
    background-position: center;
    overflow: hidden;
    position: relative;
    .titleBar{
      width: 100%;
      height: 0.96rem;
      padding-top: 1rem;
      position: relative;

      .backIcon{
        position: absolute;
        bottom: 0.38rem;
        left: 0.3rem;
        width: 0.5rem;
        height: 0.5rem;
        z-index: 2;
        display: flex;
        svg{
          width: 0.5rem;
          height: 0.4rem;
          fill: #fff;
        }
      }
      .title{
        position: absolute;
        top:  1.1rem;
        left: 0;
        right: 0;
        text-align: center;
        font-size: 0.36rem;
        color: #fff;
      }
    }
    .card{
      margin: 0.3rem auto 0.8rem;

      .cardImg{
        width: 6.26rem;
        height: 11.3rem;
        background: url(../../assets/images/cardDetail.png) no-repeat;
        background-size: cover;
        position: relative;
        margin: 0 auto;

        .gem{
          position: absolute;
          top: 1.05rem;
          left: 0;
          right: 0;
          margin: auto;
          width: 2.96em;
          height: 2.96em;
          background: url(../../assets/images/gemBg.png) no-repeat;
          background-size: cover;
          line-height: 2.96em;
          display: flex;
          justify-content: center;
          align-items: center;

          .imgWrap{
            width: 2.5rem;
            height: 2.5rem;
            display: flex;
            justify-content: center;
            align-items: center;
            border-radius: 1.25rem;
            overflow: hidden;
            img{
              max-width: 100%;
              max-height: 100%;
              width: auto;
              height: auto;
            }
          }
          
        }
        .gemDetail{
          position: absolute;
          top: 4.38rem;
          left: 0;
          right: 0;
          margin: auto;
          width: 4rem;

          .gemTitle{
            font-family: 'KaiTi', 'STKaiTiSC-Regular';
            font-size: 0.48rem;
            color: #700C0C;
            text-align: center;
            line-height: 1;
            margin-bottom: 0.28rem;
            
          }
          .content{
            height: 3.4rem;
            overflow-x: hidden;
            overflow-y: scroll;
            font-size: 0.28rem;
          }
        }
        .coin{
          display: flex;
          align-items: center;
          position: absolute;
          bottom: 1rem;
          left: 50%;
          transform: translateX(-50%);
          color: #D39B46;
          
          
          ._value{
            font-size: 0.3rem;
            text-indent: 0.38rem;
            line-height: 1;
            background: url(../../assets/images/coin.png) no-repeat;
            background-position: left center;
            background-size: 0.25rem 0.25rem;
          }
          ._text{
            font-family: 'KaiTi', 'STKaiTiSC-Regular';
            font-size: 0.28rem;
            line-height: 1;
          }
        }
      }
      .cardDesc{
        width: 6.2rem;
        margin: 0.2rem auto;
        color: #fff;

        dt{
          font-family: 'PingFangSC-Medium';
          font-size: 0.28rem;
          margin-bottom: 0.2rem;
        }
        dd{
          font-family: 'PingFangSC-Medium';
          font-size: 0.24rem;
          line-height: 0.36rem;
          min-height: 0.5rem;
        }
      }
      .btnWrap{
        display: flex;
        
        .btn{
          padding: 0 0.4rem;
          height: 0.55rem;
          margin: 0.16rem auto 0;
          font-size: 0.3rem;
          font-family: 'KaiTi', 'STKaiTiSC-Regular';
          font-weight: 500;
          color: #FFFFFF;
          text-align: center;
          line-height: 0.55rem;
          background: #790101;
        }
        .success{
          color: #790101;
          background: rgba(255, 255, 255, 0.4);
        }
        .collecting{
          background: #C1826B;
        }
      }
      
    }
  }
  .alertAnimate-enter-active {
    animation: bounce-in .5s;
  }
  .alertAnimate-leave-active {
    animation: bounce-in .5s reverse;
  }
  @keyframes bounce-in {
    0% {
      transform: scale(1);
    }
    30% {
      transform: scale(1.5);
    }
    100% {
      transform: scale(1);
    }
  }
  #animate{
    position: fixed;
    z-index: 101;
    top: 2.85rem;
    left: 0;
    right: 0;
    height: 7.5rem;
    transform: scale(2);
    transform-origin: center;
    // background: #000;
  }
  .getSuccess{
    position: fixed;
    z-index: 100;
    top: 2.85rem;
    top: calc(2.85rem + constant(safe-area-inset-top));
    top: calc(2.85rem + env(safe-area-inset-top));
    left: 0;
    right: 0;
    height: 7.5rem;
    >h3{
      font-family: 'SimSun';
      font-size: 0.42rem;
      color: #F7DAAB;
      text-align: center;
      margin-top: 1.3rem;
      letter-spacing: 2px;
      text-indent: 2px;
    }

    .rotateBg{
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: -1;
      background: url(../../assets/images/get_1.png) no-repeat;
      background-size: contain;
      animation: rotate 4s linear infinite;
    }
    .gemWrap{
      width: 5rem;
      height: 2.88rem;
      margin: 0.55rem auto 0;
      background: url(../../assets/images/star.png) no-repeat;
      background-size: contain;

      .gem{
        width: 2.66rem;
        height: 2.66rem;
        display: flex;
        align-items: center;
        justify-content: center;
        background: url(../../assets/images/gemBg_1.png) no-repeat;
        background-size: contain;
        margin: 0 auto;

        .gemImgWrap{
          width: 2.26rem;
          height: 2.26rem;
          background: #fff;
          border: 1px solid #FFBB50;
          border-radius: 50%;

          img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
          }
        }
      }
    }
    .gemName{
      font-size: 0.3rem;
      color: #F7D9AB;
      text-align: center;
      margin-top: 0.3rem;
      line-height: 1;
      padding: 0 1rem;
    }
  }
  .confirmBtn{
    position: fixed;
    top: 10rem;
    top: calc(10rem + constant(safe-area-inset-top));
    top: calc(10rem + env(safe-area-inset-top));
    left: 0;
    right: 0;
    margin:  auto;
    z-index: 102;
    width: 3.8rem;
    height: 0.8rem;
    background: #C6292B;
    color: #fff;
    border-radius: 0.4rem;
    font-size: 0.3rem;
    line-height: 0.8rem;
    text-align: center;
  }
  @keyframes rotate{
    from{transform: rotate(0deg)}
    to{transform: rotate(359deg)}
  }
</style>